package com.system.web.spring;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.log4j.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//import com.workflow.WorkFlowUtils;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.common.utils.DateUtil;
import com.system.domain.T_bz;
//import com.system.mapper.T_bzMapper;
import com.system.service.T_bzService;

@Controller
@RequestMapping("/t_bz")

public class T_bzController
{
	private static Logger log = Logger.getLogger(T_bzController.class.getName());

	@Resource
    private T_bzService t_bzService;

	//@Autowired
	//private WorkFlowUtils workFlowUtils;

	//@Resource
	//private T_bzMapper mapper;
	//private int id;
	//private int[] ids;
	private Pg pg;

	/**
	 * 1、获取 T_bz信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_bz")
	@ResponseBody
	public Pg getAllT_bz(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_bz t_bz = new T_bz();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_bz.setProcess_code(request.getParameter("process_code"));
	        }
			if(request.getParameter("bzcode")!=null&&!"".equals(request.getParameter("bzcode"))){
	        	t_bz.setBzcode(request.getParameter("bzcode"));
	        }
			if(request.getParameter("bzname")!=null&&!"".equals(request.getParameter("bzname"))){
	        	t_bz.setBzname(request.getParameter("bzname"));
	        }
			if(request.getParameter("bzmemo")!=null&&!"".equals(request.getParameter("bzmemo"))){
	        	t_bz.setBzmemo(request.getParameter("bzmemo"));
	        }
			if(request.getParameter("bzstatus")!=null&&!"".equals(request.getParameter("bzstatus"))){
	        	t_bz.setBzstatus(request.getParameter("bzstatus"));
	        }
			if(request.getParameter("creater")!=null&&!"".equals(request.getParameter("creater"))){
	        	t_bz.setCreater(request.getParameter("creater"));
	        }
			if(request.getParameter("updatetime")!=null&&!"".equals(request.getParameter("updatetime"))){
	        	t_bz.setUpdatetime(request.getParameter("updatetime"));
	        }
			if(request.getParameter("updater")!=null&&!"".equals(request.getParameter("updater"))){
	        	t_bz.setUpdater(request.getParameter("updater"));
	        }
			if(request.getParameter("systemflag")!=null&&!"".equals(request.getParameter("systemflag"))){
	        	t_bz.setSystemflag(request.getParameter("systemflag"));
	        }
			if(request.getParameter("ext1")!=null&&!"".equals(request.getParameter("ext1"))){
	        	t_bz.setExt1(request.getParameter("ext1"));
	        }
			if(request.getParameter("ext2")!=null&&!"".equals(request.getParameter("ext2"))){
	        	t_bz.setExt2(request.getParameter("ext2"));
	        }
			if(request.getParameter("ext3")!=null&&!"".equals(request.getParameter("ext3"))){
	        	t_bz.setExt3(request.getParameter("ext3"));
	        }
      	    if(request.getParameter("id")!=null&&!"".equals(request.getParameter("id"))){
	        	t_bz.setId(new Integer(request.getParameter("id")));
	        }
	        if(request.getParameter("starttime")!=null&&!"".equals(request.getParameter("starttime"))){
	        	t_bz.setStarttime(DateUtil.parseByDefaultDatePattern(request.getParameter("starttime")));
	        }
	        if(request.getParameter("endtime")!=null&&!"".equals(request.getParameter("endtime"))){
	        	t_bz.setEndtime(DateUtil.parseByDefaultDatePattern(request.getParameter("endtime")));
	        }

	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_bz.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_bz.setEnddate(request.getParameter("enddate"));
	        }
	        t_bz.setStartNo( startIndex );
	        t_bz.setEndNo( startIndex + pageSize );
	    	List<T_bz> findAll = t_bzService.findAll(t_bz);
	        int totalCount = t_bzService.findCountAll(t_bz);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_bzList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_bzList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_bz信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_bz")
	@ResponseBody
	public JsonVO saveT_bz(T_bz t_bz, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(t_bzService.save(t_bz)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 3、修改T_bz信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_bz
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_bz")
	@ResponseBody
	public JsonVO updateT_bz(T_bz t_bz, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊值需要处理，在此处手工转换
		//T_bz t_bzTemp = t_bzService.findById(t_bz);
		//t_bzTemp.setXXX(t_bz.getXXX());
		//if(t_bzService.update(t_bzTemp)){

		if(t_bzService.update(t_bz)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 4、根据id查询T_bz单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_bz json格式字符串
	 */
	@RequestMapping("/getT_bz")
	@ResponseBody
	public T_bz getT_bz(Integer id,HttpServletRequest request){
		T_bz t_bz = new T_bz();
		t_bz.setId(id);
		return t_bzService.findById(t_bz);
	}

	/**
	 * 5、删除T_bz根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_bz")
	@ResponseBody
	public JsonVO delT_bz(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_bz t_bz = new T_bz();
		t_bz.setId(id);

		if(t_bzService.delete(t_bz)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_bz/toT_bzList查询所有数据页面
	 *  return /WEB-INF/views/system/t_bz/t_bzList.jsp
	 */
    @RequestMapping(value = "/toT_bzList")
    public String toT_bzList() {
    	log.debug("------跳转到/WEB-INF/views/system/t_bz/t_bzList.jsp------");
        return "system/t_bz/t_bzList";
    }

	/**
	 * 7、新增、修改、详情页面t_bz/toT_bzCurd跳转
     * system/t_bz/T_bzCurd.jsp
	 */
    @RequestMapping(value = "/toT_bzCurd")
    public String toT_bzCurd() {
    	log.debug("------跳转到/WEB-INF/views/system/t_bz/t_bzCurd.jsp------");
        return "system/t_bz/t_bzCurd";
    }

    /**
	 * 8、批量删除T_bz根据主键id
	 * 是否要实现批量删除，待考虑，基本上用途不大
	 * @param id
	 * @param request
	 * @param response
	@RequestMapping("/delT_bzBatch")
	public String delT_bzBatch(HttpServletRequest request,HttpServletResponse response){
		String[] ids = request.getParameterValues("ids");
		System.out.println("------ids="+ids);
		T_bz t_bz = new T_bz();
		int j=0;
		for(int i =0;i< ids.length;i++){
			j++;
			log.error("------ids[i]="+ids[i]);
			t_bz.setId(new Integer(ids[i]).intValue());
			t_bzService.delete(t_bz);
		}
		if(j==ids.length){
			log.debug("------t_bz批量删除成功！------");
		}else{
			log.error("------t_bz批量删除部分成功！部分失败！------");
		}

		return "redirect:/t_bz/getAllT_bz";
	}
*/



}









